<template>
    <div :class="className" :style="{ height: height, width: width }" />
</template>

<script>
import * as echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import resize from './resize'

const animationDuration = 6000

export default {
    mixins: [resize],
    props: {
        className: {
            type: String,
            default: 'chart'
        },
        width: {
            type: String,
            default: '100%'
        },
        height: {
            type: String,
            default: '240px'
        },
        chartData: {
            type: Array,
            // required: true
        }
    },
    data() {
        return {
            chart: null,
        }
    },
    watch: {
        chartData: {
            deep: true,
            handler(val) {
                // this.setOptions(val)
            }
        }
    },
    mounted() {
        this.$nextTick(() => {
            this.initChart()
        })
    },
    beforeDestroy() {
        if (!this.chart) {
            return
        }
        this.chart.dispose()
        this.chart = null
    },
    methods: {
        initChart() {
            this.chart = echarts.init(this.$el, 'macarons')
            // this.setOptions(this.chartData)
            this.setOptions()
        },
        setOptions(numData) {
            this.chart.setOption({
                tooltip: {
                    trigger: 'axis',
                    axisPointer: {
                        type: 'cross',
                        crossStyle: {
                            color: '#fff'
                        }
                    }
                },
                grid: {
                    top: '12%',
                    left: '3%',
                    right: '4%',
                    bottom: '12%',
                    containLabel: true
                },
                legend: {
                    bottom: 'bottom',
                    textStyle: { //图例文字的样式
                        color: '#fff',
                        fontSize: 16
                    },
                },
                xAxis: [
                    {
                        type: 'category',
                        data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'],
                        axisPointer: {
                            type: 'shadow'
                        }
                    }
                ],
                yAxis: {
                    type: 'value'
                },

                series: [
                    {
                        name: '耗电量',
                        type: 'bar',
                        barWidth: '30',
                        tooltip: {
                            valueFormatter: function (value) {
                                return value + ' ml';
                            }
                        },
                        data: [
                            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
                        ],
                        itemStyle: {
                            color: {
                                type: 'linear',
                                x: 0,
                                y: 0,
                                x2: 0,
                                y2: 1,
                                colorStops: [
                                    { offset: 0, color: 'rgba(14, 236, 228, 1)' }, // 0% 处的颜色
                                    { offset: 1, color: 'rgba(5, 143, 231, 1)' } // 100% 处的颜色
                                ],
                                global: false // 缺省为 false
                            },
                        },
                    },
                    {
                        name: '耗水量',
                        type: 'line',
                        tooltip: {
                            valueFormatter: function (value) {
                                return value + ' ml';
                            }
                        },

                        data: [
                            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
                        ],
                        itemStyle: {
                            color: 'rgba(230, 247, 255, 0.40)'
                        }
                    },
                    {
                        name: '耗药量',
                        type: 'line',
                        tooltip: {
                            valueFormatter: function (value) {
                                return value + ' °C';
                            }
                        },
                        areaStyle: {},
                        data: [
                            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
                        ],
                        itemStyle: {
                            color: {
                                type: 'linear',
                                x: 0,
                                y: 0,
                                x2: 0,
                                y2: 1,
                                colorStops: [
                                    { offset: 0, color: 'rgba(71, 229, 229, 1)' }, // 0% 处的颜色
                                    { offset: 1, color: 'rgba(71, 229, 229, 0.06)' } // 100% 处的颜色
                                ],
                                global: false // 缺省为 false
                            },
                        }
                    }
                ]

            })
        }
    }
}
</script>
